import os, sys

def main(a):
  s = open("%s.txt" %(os.path.basename(a)), mode='w', encoding='utf8')
  s3= sorted(os.listdir(a))
  for i in s3:
    if i[-4:]!=".txt":
      continue
    i = os.path.join(a, i)
    with open(i, mode='r', encoding='utf8') as s5:
      s2= s5.read()
      s.write("%s " %s2)
  #s.close()

if __name__ == "__main__":
  if len(sys.argv[1:]) ==0:
    print("将多个txt文件合并。请把txt文件放入文件夹中并将此文件夹作为参数")
    sys.exit()
  for i in sys.argv[1:]:
    i= i.strip('\'\"\\').rstrip("/")
    if not os.path.isdir(i):
      print("请把txt文件放入文件夹中并将此文件夹作为参数")
      sys.exit()
    main(i)

